package homeWork;

import java.util.Scanner;

public class homeWork8 {
    public static void main(String[] args){
     //斐波那契数列的第n项（递归实现）
        System.out.println("请输入斐波那契项：");
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int result = fibonacci(n);
        System.out.println("斐波那契数列的第" + n + "项为：" + result);
    }
    public static int fibonacci(int n){
        if (n <= 0){
            return 0;
        }else if (n == 1){
            return 1;
        }
        int a = 0;
        int b =1;
        int fib = 0;
        for (int i = 2; i <= n ; i++) {
            fib = a + b;
            a = b;
            b = fib;
        }
        return fib;
    }
}
